/*
 * AnimattedModelObject.cpp
 *
 *  Created on: 03.10.2012
 *      Author: caffeine
 */

#include "AnimattedModelObject.h"
#include <log/Log.h>
#include <animation/exception/AnimationException.h>

namespace irr{
namespace object{

AnimattedModelObject::AnimattedModelObject(ContainerNodeObject*container,irr::animation::IAnimation*ag,irr::IrrlichtDevice*device):irr::ILoadableObject(device),ModelObject(container,device){
	currentAnimationRecord=0;
	animationGroup=0;
	setAnimationGroup(ag);
}

AnimattedModelObject::~AnimattedModelObject() {
	if (currentAnimationRecord)
		currentAnimationRecord->drop();
}

void AnimattedModelObject::setCurrentAnimation(irr::animation::IAnimationRecord*record){
	//if (record->getParent()!=animationGroup)
		//GEN_ANIMATION_ERROR("");
	if (currentAnimationRecord==record)
		return;
	if (currentAnimationRecord)
		currentAnimationRecord->drop();
	currentAnimationRecord=record;
	if (currentAnimationRecord)
		currentAnimationRecord->grab();
	if (isLoaded())
		on_setCurrentAnimation(record);
}

void AnimattedModelObject::on_load(){
	ModelObject::on_load();
	if (getAnimationGroup()){
		on_setAnimationGroup(getAnimationGroup());
		if (getCurrentAnimation())
			on_setCurrentAnimation(getCurrentAnimation());
	}
}

irr::animation::IAnimationRecord*AnimattedModelObject::getCurrentAnimation(){
	return currentAnimationRecord;
}

irr::animation::IAnimation*AnimattedModelObject::getAnimationGroup(){
	return animationGroup;
}

void AnimattedModelObject::setAnimationGroup(irr::animation::IAnimation*group){
	if (group==animationGroup)
		return;
	if (animationGroup)
		animationGroup->drop();
	animationGroup=group;
	if (animationGroup)
		animationGroup->grab();
	if (isLoaded())
		on_setAnimationGroup(animationGroup);
	setCurrentAnimation(0);
}

}//namespace object
}//namespace irr
